<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>短信群发</title>
	<link href="css/global.css" type="text/css" rel="stylesheet" />
	<link href="css/style.css" type="text/css" rel="stylesheet" />
	<script type="text/javascript" charset="UTF-8" src="js/jquery.js"></script>
	<script type="text/javascript" src="js/My97DatePicker/WdatePicker.js" language="javascript"></script>
	
	
	<style type="text/css"> 
html {
	overflow-y: auto !important;
	overflow-x: hidden; *
	overflow: hidden;
}
 
table td {
	text-align: left;
}
 
a {
	color: Black;
}
 
table td,table th {
	border: none;
	line-height: 20px;
}
 
.button1 {
	background: #fff url(images/send1.gif) no-repeat;
	height: 64px;
	text-align: center;
	width: 80px;
	margin-right: 5px;
	line-height: 22px;
	border: none;
}
 
.button1:hover {
	background: #fff url(images/send2.gif) no-repeat;
	border: none;
}
 
.button2 {
	background: #fff url(images/btnbg.png) no-repeat;
	height: 22px;
	text-align: center;
	width: 65px;
	margin-right: 5px;
	line-height: 22px;
	border: none;
}
 
.font1 {
	color: #666666
}
 
#channelDiv a {
	cursor: hand;
}
 
#channelDiv a {
	font-size: 12px;
}
 
#tdSetTime a {
	cursor: hand;
	text-decoration: none;
}
 
#tdSetTime a:hover {
	text-decoration: none;
}
</style>
<script type="text/javascript"> 
$(document).ready(function(){
	//加载客户信息
	initCustomerSelection();
});
var sendMobiles = "";
var customerID = "";
function checkword(){
	calcword();
	var smscontent = $("#smscontent").val();
	smscontent = smscontent.replace(/<(script|link|style|iframe)(.|\n)*\/\1>\s*/ig, "");
	if(smscontent.length>1000){
		alert("输入字数超过1000，请删减.");
		$("#smscontent").html(smscontent.substr(0, 1000));
		calcword();
		return false;
	}
}
function calcword() {
	var len = parseInt(calclength($("#smscontent").val()));
	if ($("#userCheckbox").attr("checked")) {
		if ($("#txtSignName").val().length > 0) {
			len = len + parseInt(calclength($("#txtSignName").val())) + 2;
		}
	}
	$("#wordlabel").html(len);
	if($("#wordlabel").html()==""){$("#wordlabel").html("0");}
}
 
function calclength(s) {
	var count = 0;
	for (var i = 0; i < s.length; i++) {
		if (s.charAt(i) >= ' ' && s.charAt(i) < '~') {
			count++;
		} else {
			count++;
		}
	}
	return count;
}
function changeCustomerID(customer)
{
	$("#channelDiv").html("");
	if(customer!=-1)
	{
		customerID = customer;
	}else{
		alert("请选择要发送该短信的客户.");
		$("#btnSendSms").removeAttr("disabled");	
		return;	
	}
	initialChannel();
}

function initCustomerSelection()
{
	var customerSelections = "<select id=\"customerSelection\" name=\"customerSelection\" onchange =\"changeCustomerID(this.options[this.options.selectedIndex].value)\">";
		customerSelections += "<option value=\"-1\" selected=\"selected\">请选择客户</option>";
	$.ajax({
		url:"userinfolist_ajax.do",
		type:"post",
		data:"gid="+$("#gid").val(),
		dataType:"json",
		async:false,
		success:function(data){	
			var dataList = data.dataList;
			$.each(dataList, function (i){
				customerSelections+="<option value=\""+dataList[i].id+"\" >"+dataList[i].username+"</option>"
			});
		}
	 });
	 customerSelections +="</select>";
	 var customerList = "<div style=\"padding:3px; text-align:center;\">客户选择:"+customerSelections+"</div><div id=\"channelDiv\"></div>";
	 $("#CustomerDiv").html(customerList);
}
 
 
function initialChannel(){
	var chiChannelList = "",uniChannelList = "",telChannelList = "";
 	chiChannelList += "<div style='padding-left:15px;'><input type='radio'  name='rdo1' value='1'/><a herf='#' onclick=\"setCheckBox(this,'rdo1')\">中国移动</a></div>";
	uniChannelList += "<div style='padding-left:15px;'><input type='radio'  name='rdo2' value='2'/><a herf='#' onclick=\"setCheckBox(this,'rdo2')\">中国联通</a></div>";
	telChannelList += "<div style='padding-left:15px;'><input type='radio'  name='rdo3' value='3'/><a herf='#' onclick=\"setCheckBox(this,'rdo3')\">中国电信</a></div>";
	var channelInfoStr = "";
	channelInfoStr = "<div><div  style='margin-bottom:5px;margin-top:5px;'>移动</div>" + chiChannelList + "</div>";
	channelInfoStr += "<div><div  style='margin-bottom:5px;margin-top:5px;'>联通</div>" + uniChannelList + "</div>";
	channelInfoStr += "<div><div style='margin-bottom:5px;margin-top:5px;'>电信</div>" + telChannelList + "</div>";
	channelInfoStr="<div class=\"tdtop\">通道选择</div><div style=\"padding-left:5px;\">"+channelInfoStr+"<div>";
	$("#channelDiv").html(channelInfoStr);
	$("#channelDiv").find("input[name='rdo1']").eq(0).attr("checked",true);
	$("#channelDiv").find("input[name='rdo2']").eq(0).attr("checked",true);
	$("#channelDiv").find("input[name='rdo3']").eq(0).attr("checked",true);
}
	function setCheckBox(obj,name){
		var checkbox = $(obj).prev();
		$("#channelDiv").find("input[name='"+name+"']").each(function(){
			$(this).attr("checked",false);
		});
		checkbox.attr("checked",true);
	}
	function sendsms(){
		checkword();
		var param;
		var smscontent=$("#smscontent").val();
		if (smscontent== "") {
            alert("请输入发送内容!");
            $("#smscontent").focus();
			return false;
        }
        param="contents="+smscontent
        if ($("#userCheckbox").attr("checked")) {
            if ($("#txtSignName").val().length == 0) {
                alert("请添加签名!"); 
                $("#txtSignName").focus();
				return false;
            }else if($("#txtSignName").val().length > 8 ){
            	alert("签名限制8个字符.");
            	return false;
            }
            param+="【"+$("#txtSignName").val()+"】";
        }
        var customerSelection=$("#customerSelection").val();
        if(customerSelection<0){
        	alert("请选择客户.");
            return false;
        }
        param+="&id="+customerSelection;
        if (!$(obj).attr("checked")) {
			if($("#sendtime").html()==""){
			   		alert("请选择定时时间.");
			   		return false;
			}
	        param+="&stype=2&sendtime="+$("#sendtime").html();
        }else{
        	param+="&stype=1&sendtime=";
        }
        param+="&priority="+$("#priority").val();
		var sendObject = $("#mobilelist").val();
		var sendObject = sendObject.replace(/\r\n/g,",").replace(/\n/g,",").replace(/;/g,",").replace(new RegExp("；",'g'),",").replace(new RegExp("，",'g'),",").replace(new RegExp("、",'g'),",");
		sendObject = sendObject.replace(/(^\s*)|(\s*$)/g,"");
		sendObject = sendObject.replace(/\s+/g," ");
		sendObject = sendObject.replace(/\s/g,",");
		sendObject = sendObject.replace(/,+/g,",");
		if(sendObject.endWith(",")){
			sendObject = sendObject.substring(0,sendObject.length-1);
		}
		if (sendObject == "") { alert("请填加手机号码！"); $("#mobilelist").focus();return false; }
		param+="&mobiles="+sendObject;
		var sendArrary=sendObject.split(",");
		var errorMobileList = "",validMobileList = "",repeatMobileList = "";
		for(var i=0;i<sendArrary.length;i++){						
				var mobile=sendArrary[i];
				var len=mobile.length;
				if (isNaN(mobile)){						
					errorMobileList +=mobile+",";						
				}else{						
					if(len == 12){
						if(!mobile.startWith("0")){								
							errorMobileList +=mobile + ",";									
						}else{
							if(validMobileList.indexOf(mobile) == -1){
								validMobileList +=mobile + ",";
							}else{
								repeatMobileList +=mobile + ",";
							}
						}
					}else{
						if(len == 11){
							if(validMobileList.indexOf(mobile) == -1){
								validMobileList +=mobile + ",";
							}else{
								repeatMobileList +=mobile + ",";
							}
						}else{										
							errorMobileList +=mobile+",";									
						}
					}						
				}														
		}
		var infoMsg = "";
		infoMsg += "本次发送情况如下：";
		infoMsg += "\r\n--------------------------------";
		if(validMobileList !=""){
			validMobileList = validMobileList.substring(0,validMobileList.length-1);
			infoMsg += "\r\n---有效号码有：" + validMobileList.split(",").length + "个";	
			sendMobiles = validMobileList;
		}
		if(errorMobileList !=""){
			errorMobileList = errorMobileList.substring(0,errorMobileList.length-1);
			infoMsg += "\r\n---无效号码有：" + errorMobileList.split(",").length + "个";
			//infoMsg += "\r\n分别为：" + errorMobile;				
		}
		if(repeatMobileList !=""){
			repeatMobileList = repeatMobileList.substring(0,repeatMobileList.length-1);
			infoMsg += "\r\n---重复号码有：" + repeatMobileList.split(",").length + "个";
			//infoMsg += "\r\n分别为：" + repeatMobileList;				
		}
		infoMsg += "\r\n--------------------------------";
		infoMsg += "\r\n是否确定发送？";
		var c = confirm(infoMsg);
		if(!c){
			return false;
		}else{
			alert(param);
			//开始时对按钮进行加锁
			$("#btnSendSms").attr("disabled","disabled");
			$.ajax({
				url:"smsbatchsend_main.do?action=do",
				type:"post",
				data:param,
				dataType:"text",
				async:false,
				success:function(data){	
					alert(data);
				}
			 });
			//结束后对按钮进行解锁
			$("#btnSendSms").removeAttr("disabled");
		}
	}
	String.prototype.endWith=function(str){
	if(str==null||str==""||this.length==0||str.length>this.length)
	  return false;
	if(this.substring(this.length-str.length)==str)
	  return true;
	else
	  return false;
	return true;
	}
	function initialTextBox(){
		$("#mobilelist").attr("value", "");
		$("#smscontent").attr("value", "");
		$("#wordlabel").text("0");
		$("#sendtime").text("")
        $("#chkCancel").attr("checked", "");
		$("#sendtime").hide();
        $("#spanCancel").hide();
		sendMobiles = "";
	}
    function showSignName(obj) {
        if($(obj).attr("checked")){
            $(obj).next("span").show();
        }else{
            $(obj).next().hide();
            $(obj).next().children().attr("value", "");
        }
        checkword();
    }
    function showCancel() {
        $("#sendtime").show();
        $("#spanCancel").show();
        $("#chkCancel").attr("checked", "checked");
    }
    function cancelOnTime(obj) {
        if (!$(obj).attr("checked")) {
            $(obj).parent().hide();
            $(obj).parent().prev().hide();
        }
    }
</script>
</head>
<body>
		<!-- 隐藏域 -->
		<input type="hidden" id="gid" value="${gid }"/>
		<div class="div1" style="margin-top: 5px;">
			<div class="position">
				<div style="float: left;">
					<img src="images/dangqian25.gif" style="float: left;" />
					<span style="padding-left: 2px;">短信发送</span>
				</div>
			</div>
			<div class="div2" style="margin-top: 2px;">
				<div>
					<table style="height: 100%; border: none;">
						<tr>
							<td colspan="2" align="left">
								<div style="padding-left: 5px;">
									短信内容
								</div>
							</td>
						</tr>
						<tr>
							<td colspan="2"
								style="padding-right: 5px; vertical-align: top; border: none;">
								<div style="width: 100%;">
									<textarea name="smscontent"
										style="width: 100%; height: 80px; border: #aaa solid 1px;"
										id="smscontent" cols="20" rows="1" onKeyDown="checkword();"
										onKeyUp="checkword();" onChange="checkword();"
										onKeyPress="checkword(); "></textarea>
								</div>
							</td>
						</tr>
						<tr>
							<td colspan="2" height="28" align="left" valign="middle">
								当前字数
								<span style="color: #00489a; padding-left: 2px; padding-right: 2px; font-weight: bold;" id="wordlabel">0</span> 个
							</td>
						</tr>
						<tr>
							<td rowspan="3" valign="top"
								style="border: #bbb solid 1px; background: #eee; width: 200px;">
								<div id="CustomerDiv">
								</div>
							</td>
							<td valign="top" bgcolor="#eeeeee"
								style="text-align: left; border-top: #bbb solid 1px; border-right: #bbb solid 1px; height: 20px;">
								<div style="padding-left: 5px;">
									<font style="font-size: 12px;">手机号码</font>
									<span class="font1" style="font-size: 12px; padding-left: 5px;">(手机号码之间用英文逗号分开，或者一行一个号码)</span>
									<span>
										<input id="Button" type='button' value='选择号码' onclick='search()' class="cmbutton" />
									</span>
								</div>
							</td>
						</tr>
						<tr>
							<td bgcolor="#eeeeee"
								style="padding-right: 5px; vertical-align: top; border-right: #bbb solid 1px; text-align: center;">
								<textarea name="mobilelist"
									style="width: 98%; height: 170px; border: #cccccc solid 1px;"
									id="mobilelist" cols="20" rows="1"></textarea>
							</td>
						</tr>
						<tr>
							<td bgcolor="#eeeeee"
								style="height: 100px; vertical-align: top; border-right: #bbb solid 1px; border-bottom: #bbb solid 1px;">
								<table style="height: 100%; width: 100%; border: none;">
									<tr>
										<td>
											<table width="100%" style="border: none;">
												<tr>
													<td style="padding-left: 5px; width: 150px;">
														发送优先级：
														<select id="priority" name="D1">
															<option value="2" selected="selected">
																普通
															</option>
															<option value="1">
																优先
															</option>
															<option value="0">
																置顶
															</option>
														</select>
													</td>
													<td style="padding-left: 5px;">
														<input style="border: none;" id="userCheckbox"
															type="checkbox" onclick="showSignName(this)" />
														用户签名
														<span style="display: none; padding-left: 5px;"> <input
																id="txtSignName" type="text" onKeyDown="checkword();"
																onKeyUp="checkword();" onKeyPress="checkword(); " /> <font color="red">限8个字符</font></span>
													</td>
													<td>
														&nbsp;
													</td>
												</tr>
											</table>
										</td>
										<td rowspan="2">
											<input id="btnSendSms"
												style="float: right; margin-right: 12px;"
												onclick="sendsms()" type="button" class="button1" />
										</td>
									</tr>
									<tr>
										<td style="line-height: 25px;" id="tdSetTime">
											<span style="padding-left: 5px;">
											<a onclick="showCancel();WdatePicker({dateFmt:'yyyy-MM-dd HH:mm',el:'sendtime',skin:'default',isShowClear:false,isShowToday:false});" style="cursor: hand;">
											<img align="absmiddle" alt="设置定时" src="images/clock_red.png" width="20px" height="20px"/> 设置定时</a>
											</span>
											<span style="color: red; padding-left: 5px; font-family: Arial;" id="sendtime"></span>
											<span id="spanCancel" style="display: none; padding-left: 5px;"> 
											<input id="chkCancel" type="checkbox" checked="checked" onclick="cancelOnTime(this);" /> 取消</span>
										</td>
									</tr>
									<tr>
										<td colspan="2" style="text-align: center;">
										</td>
									</tr>
								</table>
							</td>
						</tr>
					</table>
				</div>
			</div>
		</div>
	</body>
</html>